<?php
namespace Admin\Controller;

class GoodsController extends CommonController {
	public function index(){
        if(!empty($_GET['name'])) $map['name'] = array('like',"%{$_GET['name']}%");
        $list = D('Type');
        $total = $list->where($map)->count();
        $page = new \Think\Page($total,8);
        $Typelist = $list->where($map)->order("concat(path,id)")->limit($page->firstRow.','.$page->listRows)->select();
        $pageButton = $page->show();

        $this->assign('total',$total);
        $this->assign('pageButton',$pageButton);
        $this->assign('list',$Typelist);
        $this->display('index');
    }
	public function genadd(){
		$data['pid'] = 0;
		$data['path'] = '0,';
		$data['name'] = '根分类';		
		if(!empty($_GET)){
			$data['pid'] = $_GET['pid'];
			$data['path'] = $_GET['path'];
			$data['name'] = $_GET['name'];
		}
		if(IS_POST){
			$goods = D('Type');
            if($data){
                $goods->add($_POST);
                $this->redirect('index',2);
            }
        }else{
        	$this->assign('data',$data);
            $this->display();
        }
	}
    
    public function delete(){
        $id = $_GET['id'];

        $list = D('Type');
        $row = $list->where("pid = $id")->find();
        if($row){
        	$this->error('删除失败，该目录含有子分类！');
        }else{
        	$row = $list->query("select * from goods where typeid = '{$id}' or typeid in(select id from type where path like '%,$id,%')");
        	if($row){
        		$this->error('删除失败，该目录下有商品存在！');
        	}else{
        		$list->delete($id);
        	}
        }
        $this->redirect('index');
    }

    public function genedit(){
        if(IS_POST){
            $Type = D('Type');
            $Type->save($_POST); 

            $this->redirect('index',3);
        }else{
            $id = $_GET['id'];
            $Type = D('Type');
            $data = $Type->find($id);
            $this->assign('data',$data);
            $this->display('genedit');
        }
    }
//===============================添加商品====================================
    public function add(){ 
        if(IS_POST){
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->autoSub   =   false;
			 $info   =   $upload->upload();// 上传文件
             dump($info);
             die();
				if(!$info) {		// 上传错误提示错误信息   
			 		$this->error($upload->getError());
			 	}else{  
			 		foreach($info as $file){       
			 		 	$img = './Uploads/'.$file['savename'];
			 		    $image = new \Think\Image;
                        $image->open($img);
                        $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                        $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                        $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);
                    }
					//接收表单数据
					$_POST['addtime'] = time();
					$_POST['picname'] = $file['savename'];
		            if($_POST){
                        $goods = D('Goods');
		                $goods->add($_POST);
		                $this->redirect('listindex');
		            }
			 	}
		 	
        }else{
        	   $type = D('Type');
               $list = $type->getType();
               $this->assign('list',$list);            
     	       $this->display();
        }
        
    }
    public function listindex(){
    	$list = D('Goods');
        $total = $list->count();

        $typelist = $list->query("select g.* ,t.name from goods g,type t where g.typeid=t.id");
        $this->assign('total',$total);
        $this->assign('data',$typelist);
        $this->display('list');
    }
    public function del(){
        $id = $_GET['id'];

        $list = D('Goods');
        $data = $list->find($id);
        unlink ('./Uploads/'.$data[picname]);
        unlink ('./Uploads/s_'.$data[picname]);
        unlink ('./Uploads/m_'.$data[picname]);
        $list->delete($id);
        $this->redirect('listindex');
    }
    public function edit(){
    	if(IS_POST){
            if($_FILES['picname']['name']){
                $data['picname'] = $_POST['oldpicname'];
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728 ;// 设置附件上传大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->autoSub   =   false;
            $info   =   $upload->upload();// 上传文件
            if(!$info) {        // 上传错误提示错误信息   
                $this->error($upload->getError());
            }else{  
                foreach($info as $file){       
                    $img = './Uploads/'.$file['savename'];
                    $image = new \Think\Image;
                    $image->open($img);
                    $image->thumb(360, 420)->save('./Uploads/m_'.$file['savename']);
                    $image->thumb(320, 240)->save('./Uploads/s_'.$file['savename']);
                    $image->thumb(100, 100)->save('./Uploads/v_'.$file['savename']);

                    unlink ('./Uploads/'.$data[picname]);
                    unlink ('./Uploads/s_'.$data[picname]);
                    unlink ('./Uploads/m_'.$data[picname]);
                    $data['picname'] = $file['savename'];
                }
            }
        }else{
            $data['picname'] = $_POST['oldpicname'];
        }
                
            //接收表单数据
            $data['id'] = $_POST['id'];
            $data['typeid'] = $_POST['typeid'];
            $data['goods'] = $_POST['goods'];
            $data['shop_name'] = $_POST['shop_name'];
            $data['price'] = $_POST['price'];
            $data['store'] = $_POST['store'];
            $data['descr'] = $_POST['descr'];
			$data['addtime'] = time();
            $type = D('Goods');
            $type->save($data);
            $this->redirect('listindex');
        }else{

            $type = D('Type');
            $list = $type->getType();
            $this->assign('list',$list);

            $id = $_GET['id'];
            $goods = D('Goods');

            $data = $goods->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }

    //======================添加小图片==========================================
    public function addimg(){
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类  
           $upload->saveName = 'uploadName';  
           $upload->maxSize   =     3145728 ;// 设置附件上传大小   
           $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->autoSub = false;  
           $upload->rootPath = 'Uploads/';
           $upload->savePath  = 'Goods/' ; // 设置附件上传目录    // 上传文件  
           $info   =   $upload->upload();   
           //$upload->saveName = 'time';
           if(!$info) {// 上传错误提示错误信息    
           $this->error($upload->getError());
               }else{// 上传成功   

            $picsql = D('goods_img');
            $hehe['goodsid'] = $_POST['id'];
            $hehe['addtime'] = time();
            foreach($info as $val){
                $hehe['picname'] = $val['savename'];
                $result = $picsql->data($hehe)->add();

            }

            }
             $this->redirect('listindex'); 
        }else{
             $type = D('Type');
            $list = $type->getType();
            $this->assign('list',$list);
            $id = $_GET['id'];
            $goods = D('Goods');
            $data = $goods->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }

    public function Goods_detail(){
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传类  
           $upload->saveName = 'uploadName';  
           $upload->maxSize   =     3145728 ;// 设置附件上传大小   
           $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->autoSub = false;  
           $upload->rootPath = 'Uploads/';
           $upload->savePath  = 'Goods/' ; // 设置附件上传目录    // 上传文件  
           $info   =   $upload->upload();   
           //$upload->saveName = 'time';
           if(!$info) {// 上传错误提示错误信息    
           $this->error($upload->getError());
               }else{// 上传成功   
             
            $picsql = D('Goods_detail');
            $hehe['goodsid'] = $_POST['id'];
            $hehe['addtime'] = time();
            foreach($info as $val){
                $hehe['picname'] = $val['savename'];
                $result = $picsql->data($hehe)->add();

            }

            }
             $this->redirect('listindex'); 
        }else{
             $type = D('Type');
            $list = $type->getType();
            $this->assign('list',$list);
            $id = $_GET['id'];
            $goods = D('Goods');
            $data = $goods->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }
        
}